package com.thoughtworks;
import java.sql.*;
import java.util.List;

import static com.thoughtworks.DatabaseUtil.validateTableExist;

public class AccountRepository {

    public AccountRepository() {
        String buildAccountSql = "CREATE TABLE account(name VARCHAR(100) NOT NULL, mobileNumber VARCHAR(50) NOT NULL, " +
                "emailAddress VARCHAR(50) NOT NULL, key1 VARCHAR(50) NOT NULL)ENGINE=InnoDB DEFAULT CHARSET=utf8";
        String dropAccountSql = "DROP TABLE account";
        try{
            Connection conn = DatabaseUtil.getConnection();
            Statement stmt = conn.createStatement();
            if(!validateTableExist("account")){
                System.out.println("account not exist, now create a new one.");
            } else {
                System.out.println("account already exist, now drop it and create new one.");
                stmt.execute(dropAccountSql);
            }
            stmt.execute(buildAccountSql);
            conn.close();
            stmt.close();
        } catch (SQLException e){
            e.printStackTrace();
        }
    }

    public String accountRegister(String registerInfo){
        String[] info = registerInfo.split(",");
        try{
            Connection conn = DatabaseUtil.getConnection();
            String sql = "INSERT INTO account(name, mobileNumber, emailAddress, key1)"+
                        "values("+"?, ?, ?, ?)";
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, info[0]);
            ptmt.setString(2, info[1]);
            ptmt.setString(3, info[2]);
            ptmt.setString(4, info[3]);
            ptmt.execute();
            conn.close();
            ptmt.close();
            return info[0] + ",恭喜你注册成功congrats!";
            }catch (SQLException e){
            e.printStackTrace();
            return "Connection failed when register";
        }
    }

    public String accountLogIn(String logInInfo){
        String[] info = logInInfo.split(",");
        try{
            Connection conn = DatabaseUtil.getConnection();
            String sql = "SELECT * FROM account WHERE name=?";
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, info[0]);
            ResultSet rs = ptmt.executeQuery();
            String key = "";
            String mobileNumber = "";
            String emailAddress = "";
            while(rs.next()){
                key = rs.getString("key1");
                mobileNumber = rs.getString("mobileNumber");
                emailAddress = rs.getString("emailAddress");
            }
            if(key.equals(info[1])){
                String logSucceed = String.format("%s，欢迎回来welcome back！\n" +
                        "您的手机号是%s，邮箱是%s", info[0], mobileNumber, emailAddress);
                conn.close();
                ptmt.close();
                return logSucceed;
            } else {
                conn.close();
                ptmt.close();
                return "密码或用户名错误wrong key or name\n" +
                        "请重新输入用户名和密码：";
            }
        } catch (SQLException e){
            e.printStackTrace();
            return "Connection failed when log in";
        }
    }
}
